﻿namespace ShopShell
{
    using System;
    using System.Windows;
    using Microsoft.Practices.Prism.Modularity;
    using Microsoft.Practices.Prism.UnityExtensions;
    using Microsoft.Practices.ServiceLocation;
    using Microsoft.Practices.Unity;
    using ModulesInfrastructure.Views;
    using ModuleProduct;
    using ModuleOrder;
    using ModuleCustomer;
    using ModuleCustomerEdit;
    using ModuleProductDetail;
    using ModulePlaces;
    using ModuleMock;
    using ModuleAddPlace;
    using ModuleEditPlace;
    using ModuleProductEdit;
    using ModuleCategoryAdd;
    using ModuleExternalProducers;
    using ModuleOrderEdit;
    using ModuleOrderItem;
    using ModuleExternalProducersDetail;
    using ModuleExternalProducersAdd;


    public class Bootstrapper : UnityBootstrapper
    {
        #region Overrides of Bootstrapper

        protected override void InitializeShell()
        {
            base.InitializeShell();

            base.Container.RegisterInstance((IShellWindow)this.Shell);

            ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(Container));

            if (true)
            {
                Auth dialog = new Auth();
                bool? result = dialog.ShowDialog();

                if (result.HasValue && result.Value)
                {
                    ServerConnection.Service.CurrentUser = dialog.User;
                    ServerConnection.Service.PlaceId = ServerConnection.Service.CurrentUser.PlaceId.Value;

                    Window mainWindow = (Window)this.Shell;
                    Application.Current.MainWindow = mainWindow;
                    Application.Current.MainWindow.Show();
                }
                else
                {
                    Application.Current.Shutdown();
                }
            }
            else
            {
                Window mainWindow = (Window)this.Shell;
                Application.Current.MainWindow = mainWindow;
                Application.Current.MainWindow.Show();
            }

        }

        protected override System.Windows.DependencyObject CreateShell()
        {
            return this.Container.Resolve<Shell>();
        }

        protected override void ConfigureModuleCatalog()
        {
            base.ConfigureModuleCatalog();
            ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;
            moduleCatalog.AddModule(typeof(MockModule));
            moduleCatalog.AddModule(typeof(ProductModule));
            moduleCatalog.AddModule(typeof(OrderModule));
            moduleCatalog.AddModule(typeof(OrderItemModule));
            moduleCatalog.AddModule(typeof(OrderEditModule));
          
            moduleCatalog.AddModule(typeof(CustomerModule));
            moduleCatalog.AddModule(typeof(CustomerEditModule));
            moduleCatalog.AddModule(typeof(ProductDetailModule));
            moduleCatalog.AddModule(typeof(ProductEditModule));
            moduleCatalog.AddModule(typeof(CategoryAddModule));
            moduleCatalog.AddModule(typeof(PlacesModule));
            moduleCatalog.AddModule(typeof(AddPlaceModule));
            moduleCatalog.AddModule(typeof(EditPlaceModule));

            moduleCatalog.AddModule(typeof(ExternalProducersModule));
            moduleCatalog.AddModule(typeof(ExternalProducersDetailModule));
            moduleCatalog.AddModule(typeof(ExternalProducersAddModule));
        }

        #endregion
    }
}
